#include<bits/stdc++.h>
#define il inline
#define ct const
#define int long long
#define N 200010
using namespace std;
//bool ppp;
il int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
il void write(int x){
	int cnt=0;char f[40];
	if(!x) putchar('0');
	if(x<0){
		x=-x;putchar('-');
	}
	while(x){
		f[cnt++]=x%10+'0';
		x/=10;
	}
	while(cnt) putchar(f[--cnt]);
}
struct node{
	int a,b;
	il bool operator<(ct node &x)ct{
		return b>x.b;
	}
	il int c(){
		return a+b;
	}
} a[N];
int n,mn,mnn,ans=1e18;
//bool pppp;
signed main(){
//	cerr<<(&ppp-&pppp)/1024.0/1024.0<<"\n";
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	n=read();
	for(int i=1;i<=n;++i) a[i]=(node){read(),read()};
	sort(a+1,a+1+n);
	mnn=a[1].c();mn=a[2].c();
	if(mnn<mn) swap(mn,mnn);
	for(int i=3;i<=n;++i){
		ans=min(ans,mn+mnn+a[i].a);
		if(a[i].c()<mnn) mnn=a[i].c();
		if(mnn<mn) swap(mn,mnn);
	}
	write(ans);
	return 0;
}

